﻿using Demo.Config;
using Demo.Models;
using Microsoft.EntityFrameworkCore;

namespace Demo
{
    public class MyDbContext: DbContext
    {
        public DbSet<Student> Students { get; set; }
        public DbSet<Teacher> Teachers { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new TeacherConfig());
            modelBuilder.ApplyConfiguration(new StudentConfig());

            base.OnModelCreating(modelBuilder);
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // 获取链接字符串
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            //FileConfigurationExtensions
            optionsBuilder.UseMySql(config.GetConnectionString("MySQL"), new MySqlServerVersion(new Version()));

        }


    }
}
